package cfg

import (
	"fmt"
	"gopkg.in/yaml.v3"
	"io/ioutil"
)

var (
	Conf *Config
)

type Host struct {
	Domain        string `mapstructure:"domain" json:"domain" yaml:"domain"`                         // http请求域名
	HttpPort      string `mapstructure:"http_port" json:"http_port" yaml:"http_port"`                // http端口
	ServiceDomain string `mapstructure:"service_domain" json:"service_domain" yaml:"service_domain"` // 稿件服务域名
	ServicePort   string `mapstructure:"service_port" json:"service_port" yaml:"service_port"`       // 稿件服务端口
}

type Config struct {
	Host Host `mapstructure:"host" json:"host" yaml:"host"` // 子服务相关配置
}

// ReadConfig 获取配置文件
func ReadConfig(path string) (*Config, error) {
	//path := "service/config/config.yaml"
	yamlFile, err := ioutil.ReadFile(path)
	if err != nil {
		return Conf, fmt.Errorf("获取配置文件错误 = %s", err.Error())
	}

	err = yaml.Unmarshal(yamlFile, &Conf)
	if err != nil {
		return Conf, fmt.Errorf("解析文件错误 = %s", err.Error())
	}
	return Conf, nil
}
